<template>
  <el-dialog :title="form.id ? '审批' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row>
        <el-col :span="6">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" placeholder="姓名" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工号" prop="code">
            <el-input v-model="form.code" placeholder="工号" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="margin-top: 20px">
        <el-col :span="24">
          <el-form-item label="提案内容" prop="content">
            <el-input v-model="form.content" placeholder="提案内容" type="textarea" :autosize="{ minRows: 6 }"
              disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-top: 20px;">
          <el-form-item label="" prop="content">
            <img v-if="form.img1 != null" :src="'/api/' + form.img1" style="width: 160px;height: 160px;" />
            <img v-if="form.img2 != null" :src="'/api/' + form.img2"
              style="width: 160px;height: 160px;margin-left: 30px;" />
            <img v-if="form.img3 != null" :src="'/api/' + form.img3"
              style="width: 160px;height: 160px;margin-left: 30px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="margin-top: 20px;margin-bottom: 20px">
        <el-col :span="24">
          <el-form-item label="反馈" prop="remark">
            <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 4 }" placeholder="反馈"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dataFormRefuse()">拒绝</el-button>
      <el-button type="primary" @click="dataFormSubmit()">采纳</el-button>
    </span>
  </el-dialog>
</template>

<script setup lang="ts" name="ProposalsDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/backoffice/proposals'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
const canSubmit = ref(true)
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  code: '',
  name: '',
  post: '',
  content: '',
  time: '',
  status: '',
  remark: '',
  img1: '',
  img2: '',
  img3: ''

});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取proposals信息
  if (id) {
    form.id = id
    getproposalsData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};
// 表单提交
const dataFormSubmit = () => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      canSubmit.value = false
      if (form.id) {
        form.status = '1'
        putObj(form)
          .then((data) => {
            useMessage().success('已采纳');
            visible.value = false
            emit('refresh');

          })
          .catch(() => {
            canSubmit.value = true
          })
      } /*else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }*/
    }
  })
}
const dataFormRefuse = () => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      canSubmit.value = false
      form.status = '2'
      putObj(form)
        .then((data) => {
          useMessage().success('已拒绝');
          visible.value = false
          emit('refresh');
        })
        .catch(() => {
          canSubmit.value = true
        })
    }
  })
}
// 初始化表单数据
const getproposalsData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>